<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="商品id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="商品名称">
        <el-input v-model="queryParams.goodsName"
                  placeholder="请点击右侧按钮选择商品"
                  @clear="clearSelectedGoods2"
                  clearable
                  disabled="disabled"
                  class="input-with-select">
          <el-button v-on:click.native="openProductSelector2()"
                     slot="append"
                     icon="el-icon-search"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="类型" prop="shareType">
        <el-select v-model="queryParams.shareType" placeholder="请选择类型">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['share:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['share:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['share:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['share:info:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <!-- <el-table-column label="商品id" align="center" prop="goodsId" /> -->
      <el-table-column label="商品名称" align="center" prop="shopGoods.goodsName" />
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <img v-if="scope.row.shopGoods" :src="scope.row.shopGoods.goodsImg" alt="" style="width: 50%;">
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="shareType" width="120" :formatter="typeFormat" />
      <el-table-column label="文案" align="center" prop="shareInfo" />
      <!-- <el-table-column label="店铺id" align="center" prop="supplierId" width="120" />
      <el-table-column label="商品店铺价格" align="center" prop="supplierPrice" width="120" />
      <el-table-column label="商店成本价格" align="center" prop="costPrice" width="120" /> -->
      <el-table-column label="备注" align="center" prop="shareRemark" />
      <!-- <el-table-column label="是否有效" align="center" prop="isActive" :formatter="isActiveFormat" width="100" />
      <el-table-column label="租户id" align="center" prop="lesseeId" width="100" /> -->
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['share:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['share:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改素材圈对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="450px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="60px">
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="是否有效">
              <el-select v-model="form.isActive" placeholder="请选择是否有效">
                <el-option
                  v-for="dict in isActiveOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户id" prop="lesseeId">
              <el-input v-model="form.lesseeId" placeholder="请输入租户id" />
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-form-item label="类型" prop="shareType">
          <el-input v-model="form.shareType" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="文案" prop="shareInfo">
          <el-input type="textarea" :rows="4" v-model="form.shareInfo" placeholder="请输入文案"></el-input>
        </el-form-item>
        <el-form-item label="商品id"
                      prop="goodsId">
          <el-input v-model="form.goodsId"
                    placeholder="请点击右侧按钮选择商品"
                    @clear="clearSelectedGoods"
                    clearable
                    class="input-with-select">
            <el-button v-on:click.native="openProductSelector()"
                       slot="append"
                       icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="备注" prop="shareRemark">
          <el-input v-model="form.shareRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新增界面-选择商品信息 -->
    <el-dialog fullscreen
               append-to-body
               center
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               title="请选择1件商品"
               :visible.sync="isSelectProduct"
               :before-close="closeProductSelector">
      <product-selector isComponent="true"
                        @onSelect="onSelect"
                        @onCancel="closeProductSelector"></product-selector>
    </el-dialog>

    <!-- 搜索条件-选择商品信息 -->
    <el-dialog fullscreen
               append-to-body
               center
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               title="请选择1件商品"
               :visible.sync="isSelectProduct2"
               :before-close="closeProductSelector2">
      <product-selector isComponent="true"
                        @onSelect="onSelect2"
                        @onCancel="closeProductSelector2"></product-selector>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/share/info";
import ProductSelector from "@/components/ProductSelector";

export default {
  name: "Info",
  components: { ProductSelector },
  data() {
    return {
      isSelectProduct: false,
      isSelectProduct2: false,
      selectProducts: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 是否有效字典
      isActiveOptions: [],
      // 类型字典
      typeOptions: [],
      // 素材圈表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: undefined,
        goodsName: undefined,
        shareType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getDicts("SHOP_SHARE_INFO").then(response => {
      this.typeOptions = response.data;
      console.log(this.typeOptions);
    });
    this.getList();
  },
  methods: {
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.shareType);
    },
    clearSelectedGoods() {
      this.selectProducts = [];
      this.queryParams.goodsId = "";
      this.queryParams.goodsName = "";
      this.form.goodsId = "";
      this.form.goodsName = "";
    },
    openProductSelector() {
      const that = this;
      that.selectedList = [];
      that.isSelectProduct = true;
    },
    closeProductSelector() {
      this.isSelectProduct = false;
    },
    onSelect(selectProducts) {
      if (!selectProducts || selectProducts.length === 0) return;
      const product = selectProducts[0];
      if (this.goodsId === product.id) return;
      this.queryParams.goodsId = "";
      this.queryParams.goodsName = "";
      this.form.goodsId = product.id;
      this.form.goodsName = product.goodsName;
    },
    clearSelectedGoods2() {
      this.selectProducts = [];
      this.queryParams.goodsId = "";
      this.queryParams.goodsName = "";
    },
    openProductSelector2() {
      const that = this;
      that.selectedList = [];
      that.isSelectProduct2 = true;
    },
    closeProductSelector2() {
      this.isSelectProduct2 = false;
    },
    onSelect2(selectProducts) {
      if (!selectProducts || selectProducts.length === 0) return;
      const product = selectProducts[0];
      this.queryParams.goodsId = product.id;
      this.queryParams.goodsName = product.goodsName;
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    /** 查询素材圈列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: this.$store.state.user.name || '',
        createTime: undefined,
        updateBy: this.$store.state.user.name || '',
        updateTime: undefined,
        isActive: '1',
        lesseeId: this.$store.state.user.lesseeid || '13',
        goodsId: undefined,
        shareInfo: undefined,
        shareType: '主推款',
        shareRemark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.goodsId = '';
      this.queryParams.goodsName = '';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加素材圈";
      this.$router.push('/share/info/edit/add');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const id = row.id || this.ids
      // getInfo(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改素材圈";
      // });
      this.$router.push('/share/info/edit/' + id);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除素材圈编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有素材圈数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
